package com.findviewbydream.evil.eda.Welcome;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.findviewbydream.evil.eda.Data.Data;
import com.findviewbydream.evil.eda.R;
import com.findviewbydream.evil.eda.Utils.FontUtils;
import com.findviewbydream.evil.eda.Utils.Utils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.FileCallBack;

import java.io.File;

import is.arontibo.library.ElasticDownloadView;
import okhttp3.Call;

/********************************************
 * 工程名：Eda
 * 包名：com.findviewbydream.evil.eda.Welcome
 * 时间：16-7-10 下午7:02
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：升级下载页
 ********************************************/
public class UpdateDownload extends AppCompatActivity
{
    private static final String FILEPATH = "/storage/emulated/0/Eda";
    private static final String APPNAME = "Eda.apk";

    private ElasticDownloadView elasticDownloadView;

    private String downloadURL;
    private ImageView tag;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.updatedownload);
        initView();

        downloadURL = getIntent().getStringExtra("downloadURL");

        Glide.with(this).load(R.drawable.updatedownload).into(tag);

        update();
    }


    /**
     * 初始化控件
     */
    private void initView()
    {
        elasticDownloadView = (ElasticDownloadView) findViewById(R.id.elasticDownloadView);
        tag = (ImageView) findViewById(R.id.tag);
    }


    /**
     * 进行升级操作
     */
    private void update()
    {
        elasticDownloadView.startIntro();
        newDir(FILEPATH);
        deleteFile(FILEPATH, APPNAME);
        OkHttpUtils.get().url(downloadURL).build()
                .execute(new FileCallBack(FILEPATH, APPNAME)
                {
                    int progressInt = -1;

                    @Override
                    public void inProgress(float progress, long total, int id)
                    {
                        //Utils.Log((int) (progress * 100) + " " + total);

                        if ((int) (progress * 100) != progressInt)
                        {
                            progressInt = (int) (progress * 100);
                            elasticDownloadView.setProgress(progressInt);
                        }
                    }

                    @Override
                    public void onError(Call call, Exception e, int id)
                    {
                        elasticDownloadView.fail();
                    }

                    @Override
                    public void onResponse(File response, int id)
                    {
                        openFiles(response);
                        elasticDownloadView.success();
                        finish();
                    }
                });
    }

    /**
     * 检测文件夹是否存在
     *
     * @param dir 文件夹名称
     * @return 返回是否新建成功
     */
    private boolean newDir(String dir)
    {
        File folder = new File(dir);
        return (folder.exists() && folder.isDirectory()) || folder.mkdirs();
    }

    /**
     * 检测文件是否存在，如果存在，删除
     *
     * @return 返回是否删除成功
     */
    private boolean deleteFile(String filePath, String appName)
    {
        File file = new File(filePath + "/" + appName);
        if (file.exists())
        {
            return file.delete();
        }
        return false;
    }

    /**
     * 打开文件
     *
     * @param file
     */
    private void openFiles(File file)
    {
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Intent.ACTION_VIEW);
        //"application/vnd.android.package-archive"是apk的type
        intent.setDataAndType(Uri.fromFile(file),
                "application/vnd.android.package-archive");
        startActivity(intent);
    }


}
